package com.yuwubao.hz_International.services.impl;

import com.yuwubao.hz_International.entities.AcountDetailEntity;
import com.yuwubao.hz_International.entities.repository.AcountDetailRepository;
import com.yuwubao.hz_International.entities.vo.RechargeVo;
import com.yuwubao.hz_International.services.AcountDetailService;
import com.yuwubao.hz_International.util.PageUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by yangyu on 2018/1/29.
 */
@Service
public class AcountDetailServiceImpl implements AcountDetailService {

    @Autowired
    private AcountDetailRepository acountDetailRepository;

    @Autowired
    private JdbcTemplate jdbcTemplate;


    @Override
    public List<AcountDetailEntity> findFinancialDetails(Map<String, String> map, PageUtil pageUtil, int consumerId) {
        String start = map.get("beginTime");
        String end = map.get("endTime");
        int index = pageUtil.getIndex()-1;
        int size = pageUtil.getSize();
        String sql = "SELECT * FROM acount_detail WHERE consumerId = ?";
        if (StringUtils.isNotBlank(start)) {
            sql += " AND createTime > '" + start +"'";
            if (StringUtils.isNotBlank(end)) {
                sql += " AND createTime < '" + end + "'";
            }
        }
        if (size != 0) {
            if (index == 0) {
                sql += " LIMIT " + index + "," + size;
            } else {
                sql += " LIMIT " + index*size + "," + size;
            }
        }
        RowMapper<AcountDetailEntity> rowMapper = new BeanPropertyRowMapper<AcountDetailEntity>(AcountDetailEntity.class);
        List<AcountDetailEntity> list = jdbcTemplate.query(sql, rowMapper, consumerId);
        return list;
    }

    @Override
    public Integer findFinancialDetailsSum(Map<String, String> map, int consumerId) {
        String start = map.get("beginTime");
        String end = map.get("endTime");
        String sql = "SELECT count(*) FROM acount_detail WHERE consumerId = " + consumerId;
        if (StringUtils.isNotBlank(start)) {
            sql += " AND createTime > '" + start +"'";
            if (StringUtils.isNotBlank(end)) {
                sql += " AND createTime < '" + end + "'";
            }
        }
        Integer sum = jdbcTemplate.queryForObject(sql, Integer.class);
        return sum;
    }

    @Override
    public List<RechargeVo> findRechargeList(Map<String, String> map, PageUtil pageUtil, String consumerName) {
        String start = map.get("beginTime");
        String end = map.get("endTime");
        int index = pageUtil.getIndex() - 1;
        int size = pageUtil.getSize();
        String sql = "SELECT a.id, c.`name`," +
                " a.createTime," +
                " a.money," +
                " a.bonouse," +
                " a.payType FROM consumer c, acount_detail a " +
                " WHERE c.id = a.consumerId ";

        if (StringUtils.isNotBlank(start)) {
            sql += " AND a.createTime > '" + start +"'";
            if (StringUtils.isNotBlank(end)) {
                sql += " AND a.createTime < '" + end + "'";
            }
        }

        if (StringUtils.isNotBlank(consumerName)) {
            sql += " AND c.name LIKE '%" + consumerName + "%'";
        }

        if (size != 0) {
            if (index == 0) {
                sql += " LIMIT " + index + "," + size;
            } else {
                sql += " LIMIT " + index*size + "," + size;
            }
        }
        RowMapper<RechargeVo> rowMapper = new BeanPropertyRowMapper<RechargeVo>(RechargeVo.class);
        List<RechargeVo> list = jdbcTemplate.query(sql, rowMapper);
        return list;
    }

    @Override
    public Integer findRechargeDetailsSum(Map<String, String> map, String consumerName) {
        String start = map.get("beginTime");
        String end = map.get("endTime");
        String sql = "SELECT count(*) FROM consumer c, acount_detail a WHERE c.id = a.consumerId ";
        if (StringUtils.isNotBlank(consumerName)) {
            sql += " AND c.name LIKE '%" + consumerName + "%'";
        }
        if (StringUtils.isNotBlank(start)) {
            sql += " AND a.createTime > '" + start +"'";
            if (StringUtils.isNotBlank(end)) {
                sql += " AND a.createTime < '" + end + "'";
            }
        }
        Integer sum = jdbcTemplate.queryForObject(sql, Integer.class);
        return sum;
    }
}
